package com.smile.controller;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.smile.api.FlyApi;
import com.smile.api.PayloadApi;
import com.smile.commons.gps.CoordinateTransformUtil;
import com.smile.model.dto.request.*;
import com.smile.model.request.*;
import com.smile.model.dto.request.DroneEmergencyStopRequestDTO;
import com.smile.model.response.response.PayloadDataResponseDTO;
import com.smile.service.FlyService;
import com.smile.service.LiveService;
import com.smile.service.PayloadService;
//import com.smile.service.PayloadService2;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

@RestController
//@RequestMapping("/fly")
public class FlyController {


    @Autowired
    FlyApi flyApi;

    @Autowired
    PayloadService payloadService;


    @Autowired
    PayloadApi payloadApi;

    @Autowired
    FlyService flyService;

    @Autowired
    LiveService liveService;


    @Resource
    private SimpMessagingTemplate messagingTemplate;


    @MessageMapping("/droneControl") // 指定客户端发送消息的路径，例如：/app/hello
    public void greeting(ControlRequest controlRequest) throws Exception {
        Long timestamp = System.currentTimeMillis();
        if (controlRequest.getMethod() == null || controlRequest.getMethod().equals("droneControl")) {//飞行操控
            System.out.println("飞控操作");
            DroneControlRequest droneControlRequest = new DroneControlRequest();
            BeanUtils.copyProperties(controlRequest, droneControlRequest);
            droneControlRequest.setSeq(0);
            DroneControlRequestDTO droneControlRequestDTO = BeanUtil.copyProperties(droneControlRequest, DroneControlRequestDTO.class);
            flyService.droneControl(droneControlRequestDTO);
//            flyApi.droneControl(droneControlRequest);
        }else if (controlRequest.getMethod().equals("droneEmergencyStop")){//急停
            DroneEmergencyStopRequestDTO droneEmergencyStopRequest = new DroneEmergencyStopRequestDTO();
            droneEmergencyStopRequest.setGateway(controlRequest.getGateway());
            flyService.droneEmergencyStop(droneEmergencyStopRequest);
//            flyApi.droneEmergencyStop(droneEmergencyStopRequest);
        }else  if (controlRequest.getMethod().equals("flyToPoint")){//飞往目标点
            FlyToPointRequestDTO flyToPointRequest = new FlyToPointRequestDTO();
            flyToPointRequest.setHeight(BigDecimal.valueOf(120));
            flyToPointRequest.setMaxSpeed(BigDecimal.valueOf(12));
            flyToPointRequest.setLatitude(controlRequest.getLatitude());
            flyToPointRequest.setLongitude(controlRequest.getLongitude());
            flyService.flyToPoint(flyToPointRequest);
        }else  if (controlRequest.getMethod().equals("control")){
            Map<String,Object> map1 = new HashMap<>();
            map1.put("clientId",controlRequest.getClientId());
            map1.put("method","control");
            messagingTemplate.convertAndSend("/topic/"+controlRequest.getGateway(), JSON.toJSONString(map1));
        }else  if (controlRequest.getMethod().equals("gimbalReset")){//云台重置
            System.out.println("云台重置");
            GimbalResetRequestDTO  gimbalResetRequestDTO = new GimbalResetRequestDTO();
            gimbalResetRequestDTO.setGateway(controlRequest.getGateway());
            gimbalResetRequestDTO.setResetMode(controlRequest.getResetMode());
            PayloadDataResponseDTO payloadDataResponseDTO = payloadService.getPayloadData(controlRequest.getGateway());
            System.out.println(payloadDataResponseDTO);
            gimbalResetRequestDTO.setPayloadIndex(payloadDataResponseDTO.getPayloadIndex());
            payloadService.gimbalReset(gimbalResetRequestDTO);

        }else  if (controlRequest.getMethod().equals("cameraFocalLengthSet")){//变焦
            System.out.println("变焦");
            CameraFocalLengthSetRequestDTO cameraFocalLengthSetRequestDTO = new CameraFocalLengthSetRequestDTO();
            cameraFocalLengthSetRequestDTO.setGateway(controlRequest.getGateway());
            cameraFocalLengthSetRequestDTO.setCameraType(controlRequest.getCameraType());
            cameraFocalLengthSetRequestDTO.setZoomFactor(controlRequest.getZoomFactor());
            PayloadDataResponseDTO payloadDataResponseDTO = payloadService.getPayloadData(controlRequest.getGateway());
            cameraFocalLengthSetRequestDTO.setPayloadIndex(payloadDataResponseDTO.getPayloadIndex());
            payloadService.cameraFocalLengthSet(cameraFocalLengthSetRequestDTO);
        } else if (controlRequest.getMethod().equals("cameraScreenDrag")) { //负载控制—画面拖动控制
            CameraScreenDragRequestDTO request =  new CameraScreenDragRequestDTO();
            PayloadDataResponseDTO payloadDataResponseDTO = payloadService.getPayloadData(request.getGateway());
            request.setLocked(controlRequest.getLocked());
            request.setPitchSpeed(controlRequest.getPitchSpeed());
            request.setYawSpeed(controlRequest.getYawSpeed());
            request.setPayloadIndex(payloadDataResponseDTO.getPayloadIndex());
            request.setGateway(controlRequest.getGateway());
            payloadService.cameraScreenDrag(request);
//            payloadApi.cameraScreenDrag(request);

        } else if (controlRequest.getMethod().equals("liveLensChange")) {//切换镜头模式
            LiveLensChangeRequest liveLensChangeRequest = new LiveLensChangeRequest();
            PayloadDataResponseDTO payloadDataResponseDTO =  payloadService.getPayloadData(controlRequest.getGateway());
            if(ObjectUtil.isNotNull(payloadDataResponseDTO)){
                liveLensChangeRequest.setSn(payloadDataResponseDTO.getSn());
                liveLensChangeRequest.setGateway(controlRequest.getGateway());
//                {"ir":"红外","normal":"默认","wide":"广角","zoom":"变焦"}
                if(controlRequest.getVideoType().equals("ir")){
                    liveLensChangeRequest.setVideoType(0);
                }else if(controlRequest.getVideoType().equals("normal")){
                    liveLensChangeRequest.setVideoType(1);
                }else if(controlRequest.getVideoType().equals("wide")){
                    liveLensChangeRequest.setVideoType(2);
                }else if(controlRequest.getVideoType().equals("zoom")){
                    liveLensChangeRequest.setVideoType(3);
                }
                LiveLensChangeRequestDTO liveLensChangeRequestDTO = BeanUtil.copyProperties(liveLensChangeRequest,LiveLensChangeRequestDTO.class);
                liveService.liveLensChange(liveLensChangeRequestDTO);
//                flyApi.liveLensChange(liveLensChangeRequest);
            }
        } else if (controlRequest.getMethod().equals("cameraFrameZoom")) {//负载控制-框选变焦

            CameraFrameZoomRequestDTO cameraFrameZoomRequest = new CameraFrameZoomRequestDTO();
            PayloadDataResponseDTO payloadDataResponseDTO =  payloadService.getPayloadData(controlRequest.getGateway());
            if(ObjectUtil.isNotNull(payloadDataResponseDTO)){
                cameraFrameZoomRequest.setGateway(controlRequest.getGateway());
                cameraFrameZoomRequest.setHeight(controlRequest.getHeight());
                cameraFrameZoomRequest.setWidth(controlRequest.getWidth());
                cameraFrameZoomRequest.setCameraType(controlRequest.getCameraType());
                cameraFrameZoomRequest.setX(controlRequest.getX1());
                cameraFrameZoomRequest.setY(controlRequest.getY1());
                cameraFrameZoomRequest.setLocked(controlRequest.getLocked());
                cameraFrameZoomRequest.setPayloadIndex(String.valueOf(payloadDataResponseDTO.getPayloadIndex()));
                payloadService.cameraFrameZoom(cameraFrameZoomRequest);
//                payloadApi.cameraFrameZoom(cameraFrameZoomRequest);
            }
        } else if (controlRequest.getMethod().equals("cameraPhotoTake")) { //开始拍照
            CameraPhotoTakeRequestDTO cameraPhotoTakeRequest = new CameraPhotoTakeRequestDTO();
            PayloadDataResponseDTO payloadDataResponseDTO =  payloadService.getPayloadData(controlRequest.getGateway());
            if(ObjectUtil.isNotNull(payloadDataResponseDTO)){
                cameraPhotoTakeRequest.setGateway(controlRequest.getGateway());
                cameraPhotoTakeRequest.setPayloadIndex(payloadDataResponseDTO.getPayloadIndex());
                payloadService.cameraPhotoTake(cameraPhotoTakeRequest);
//                payloadApi.cameraPhotoTake(cameraPhotoTakeRequest);
            }
        }else if (controlRequest.getMethod().equals("cameraPhotoStop")) { //结束拍照
            CameraPhotoStopRequestDTO cameraPhotoStopRequest = new CameraPhotoStopRequestDTO();
            PayloadDataResponseDTO payloadDataResponseDTO =  payloadService.getPayloadData(controlRequest.getGateway());
            if(ObjectUtil.isNotNull(payloadDataResponseDTO)){
                cameraPhotoStopRequest.setGateway(controlRequest.getGateway());
                cameraPhotoStopRequest.setPayloadIndex(payloadDataResponseDTO.getPayloadIndex());
                payloadService.cameraPhotoStop(cameraPhotoStopRequest);
//                payloadApi.cameraPhotoStop(cameraPhotoStopRequest);
            }
        }else if (controlRequest.getMethod().equals("cameraRecordingStart")) { //开始录像
            CameraRecordStartRequestDTO cameraRecordStartRequest = new CameraRecordStartRequestDTO();
            PayloadDataResponseDTO payloadDataResponseDTO =  payloadService.getPayloadData(controlRequest.getGateway());
            if(ObjectUtil.isNotNull(payloadDataResponseDTO)){
                cameraRecordStartRequest.setGateway(controlRequest.getGateway());
                cameraRecordStartRequest.setPayloadIndex(payloadDataResponseDTO.getPayloadIndex());
                payloadService.cameraRecordingStart(cameraRecordStartRequest);
            }
        }else if (controlRequest.getMethod().equals("cameraRecordingStop")) { //结束录像
            CameraRecordStopRequestDTO cameraRecordStopRequest = new CameraRecordStopRequestDTO();
            PayloadDataResponseDTO payloadDataResponseDTO =  payloadService.getPayloadData(controlRequest.getGateway());
            if(ObjectUtil.isNotNull(payloadDataResponseDTO)){
                cameraRecordStopRequest.setGateway(controlRequest.getGateway());
                cameraRecordStopRequest.setPayloadIndex(payloadDataResponseDTO.getPayloadIndex());
                payloadService.cameraRecordingStop(cameraRecordStopRequest);
            }
        }else if (controlRequest.getMethod().equals("cameraModeSwitch")) { //负载控制—切换相机模式
            CameraModeSwitchRequestDTO cameraModeSwitchRequest = new CameraModeSwitchRequestDTO();
            PayloadDataResponseDTO payloadDataResponseDTO =  payloadService.getPayloadData(controlRequest.getGateway());
            if(ObjectUtil.isNotNull(payloadDataResponseDTO)){
                cameraModeSwitchRequest.setGateway(controlRequest.getGateway());
                cameraModeSwitchRequest.setPayloadIndex(payloadDataResponseDTO.getPayloadIndex());
                System.out.println(controlRequest.getCameraMode());
                cameraModeSwitchRequest.setCameraMode(controlRequest.getCameraMode());
                if (Objects.isNull(cameraModeSwitchRequest.getCameraMode())) {
                    cameraModeSwitchRequest.setCameraMode(0);
                }
                payloadService.cameraModeSwitch(cameraModeSwitchRequest);
            }
        }else if (controlRequest.getMethod().equals("flyToPointUpdate")) { //更新目标点
            FlyToPointUpdateRequestDTO flyToPointUpdateRequest = new FlyToPointUpdateRequestDTO();
            FlyToPointUpdateRequestDTO.Point point = new FlyToPointUpdateRequestDTO.Point();
            CoordinateTransformUtil.Coordinate coordinate =  CoordinateTransformUtil.gcj02ToWgs84(controlRequest.getLongitude(), controlRequest.getLatitude());
            point.setLongitude(coordinate.getLongitude());
            point.setLatitude(coordinate.getLatitude());
            point.setHeight(120.01F);
            List<FlyToPointUpdateRequestDTO.Point> pointList = new ArrayList<>();
            pointList.add(point);
            flyToPointUpdateRequest.setGateway(controlRequest.getGateway());
            flyToPointUpdateRequest.setPoints(pointList);
            flyToPointUpdateRequest.setMaxSpeed(10);
            System.out.println(flyToPointUpdateRequest);
            System.out.println("更新目标点");
            flyService.flyToPointUpdate(flyToPointUpdateRequest);
        } else if (controlRequest.getMethod().equals("cameraPointFocusAction")) {//点对焦
            System.out.println("点对焦");
            CameraPointFocusActionDTO cameraPointFocusAction = new CameraPointFocusActionDTO();
            PayloadDataResponseDTO payloadDataResponseDTO =  payloadService.getPayloadData(controlRequest.getGateway());
            if(ObjectUtil.isNotNull(payloadDataResponseDTO)){
                cameraPointFocusAction.setGateway(controlRequest.getGateway());
                cameraPointFocusAction.setPayloadIndex(payloadDataResponseDTO.getPayloadIndex());
                cameraPointFocusAction.setCameraType(controlRequest.getCameraType());
                cameraPointFocusAction.setX(controlRequest.getX1());
                cameraPointFocusAction.setY(controlRequest.getX1());
                payloadService.cameraPointFocusAction(cameraPointFocusAction);
            }
        } else if (controlRequest.getMethod().equals("cameraAim")) {//点对焦
            System.out.println("AIM");
            CameraAimDTO cameraAimDTO = new CameraAimDTO();
            PayloadDataResponseDTO payloadDataResponseDTO =  payloadService.getPayloadData(controlRequest.getGateway());
            if(ObjectUtil.isNotNull(payloadDataResponseDTO)){
                cameraAimDTO.setGateway(controlRequest.getGateway());
                cameraAimDTO.setPayloadIndex(payloadDataResponseDTO.getPayloadIndex());
                cameraAimDTO.setCameraType(controlRequest.getCameraType());
                cameraAimDTO.setX(controlRequest.getX1());
                cameraAimDTO.setY(controlRequest.getX1());
                cameraAimDTO.setLocked(controlRequest.getLocked());
                payloadService.cameraAim(cameraAimDTO);
            }
        }
        Long timestamp2 = System.currentTimeMillis();
        System.out.println("操作耗时:"+(timestamp2 -timestamp));
    }
}
